package com.example.club.clubmanage.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.club.clubmanage.R;

/**
 * Created by HuoSai on 2018/4/9.
 */
public class QuickParkToolbar extends Toolbar {

    public static final String TAG = "QuickParkToolbar";

    public static final int DRAWABLE_POSITION_LEFT = 1;
    public static final int DRAWABLE_POSITION_RIGHT = 2;


    //控件
    private TextView mTvTitle;
    private TextView mTvAction;
    private ImageView mIvAction;

    //属性
    private String textTitle;
    private String textAction;
    private int textSizeTitle;
    private int textSizeAction;
    private int actionIcon;
    private int colorTitle;
    private int colorAction;


    private Context mContext;

    private OnClickListener mOnClickListenerAction;

    public QuickParkToolbar(Context context,
                            @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initAttr(context, attrs);
        initView(context);
        initViewLayout();
    }

    public QuickParkToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttr(context, attrs);
        initView(context);
        initViewLayout();
    }

    private void initView(Context context) {
        if (mTvAction == null) {
            mTvAction = new TextView(context);
            mTvAction.setText(textAction);
            mTvAction.setTextColor(colorAction);
            mTvAction.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeAction);
            mTvAction.setGravity(Gravity.CENTER);
        }
        if (mTvTitle == null) {
            mTvTitle = new TextView(context);
            mTvTitle.setText(textTitle);
            mTvTitle.setTextColor(colorTitle);
            mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeTitle);
            mTvAction.setGravity(Gravity.CENTER);
        }
        if (mIvAction == null) {
            mIvAction = new ImageView(context);
            if (actionIcon != -1) {
                mIvAction.setImageResource(actionIcon);
            }
        }

        this.setTitle("");
    }

    private void initAttr(Context context, AttributeSet attrs) {
        TypedArray ta = context.getResources()
            .obtainAttributes(attrs, R.styleable.Toolbar);

        textTitle = ta.getString(R.styleable.Toolbar_toolbarTitleText);
        textAction = ta.getString(R.styleable.Toolbar_toolbarActionText);
        textSizeTitle = (int) ta
            .getDimension(R.styleable.Toolbar_toolbarTitleTextSize,
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,
                    getResources().getDisplayMetrics()));
        textSizeAction = (int) ta
            .getDimension(R.styleable.Toolbar_toolbarActionTextSize, TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_SP, 14,
                    getResources().getDisplayMetrics()));
        colorTitle = ta.getColor(R.styleable.Toolbar_toolbarTitleTextColor, Color.WHITE);
        colorAction = ta.getColor(R.styleable.Toolbar_toolbarActionTextColor, Color.WHITE);
        actionIcon = ta.getResourceId(R.styleable.Toolbar_toolbarActionIcon, -1);
        ta.recycle();
    }


    private void initViewLayout() {

        //中间的layoutParams
        LayoutParams layoutParamsCenter = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
        layoutParamsCenter.gravity = Gravity.CENTER;
        //右侧按钮的layoutparams
        LayoutParams layoutParamsRight = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.MATCH_PARENT);
        layoutParamsRight.rightMargin = dp2px(getContext(), 16);
        layoutParamsRight.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;

        mTvTitle.setLayoutParams(layoutParamsCenter);
        mTvAction.setLayoutParams(layoutParamsRight);
        mIvAction.setLayoutParams(layoutParamsRight);
        if (TextUtils.isEmpty(textAction)) {
            mTvAction.setVisibility(GONE);
        }

        if (actionIcon == -1) {
            mIvAction.setVisibility(GONE);
        }

        this.addView(mTvTitle);
        this.addView(mTvAction);
        this.addView(mIvAction);
    }

    public void setOnClickListenerAction(OnClickListener onClickListenerAction) {
        mOnClickListenerAction = onClickListenerAction;
        mTvAction.setOnClickListener(mOnClickListenerAction);
        mIvAction.setOnClickListener(mOnClickListenerAction);
    }

    public void setActionIconVisibility(int visibility) {
        mIvAction.setVisibility(visibility);
    }

    public void setActionTextVisibility(int visibility) {
        mTvAction.setVisibility(visibility);
    }

    public void setToolbarTitle(CharSequence toolbarTitle) {
        mTvTitle.setText(toolbarTitle);
    }

    public void setActionTv(CharSequence toolbarAction) {
        mTvAction.setText(toolbarAction);
    }


    public void setToolbarTitleColor(int color) {
        mTvTitle.setTextColor(color);
    }

    public void setActionTvColor(int color) {
        mTvAction.setTextColor(color);
    }

    public void setActionIv(@DrawableRes int resId) {
        mIvAction.setImageResource(resId);
    }


    public void setActionTvDrawable(@DrawableRes int resId, int position, int padding) {
        mTvAction.setCompoundDrawablePadding(dp2px(mContext, padding));
        if (position == DRAWABLE_POSITION_LEFT) {
            mTvAction
                .setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(resId), null,
                    null, null);
        } else if (position == DRAWABLE_POSITION_RIGHT) {
            mTvAction.setCompoundDrawablesWithIntrinsicBounds(null, null,
                getResources().getDrawable(resId), null);
        }
    }

    public CharSequence getTvAction() {
        return mTvAction.getText();
    }

    public void setNavigationVisibility(int visibility) {
        if (visibility == VISIBLE) {
            setNavigationIcon(R.mipmap.back);
        } else {
            setNavigationIcon(null);
        }
    }


    /**
     * dp2px
     */
    public static int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

}
